﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using System.IO;
using Atlandiz.Data;
using System.Net;
using System.Net.Sockets;
using System.Diagnostics;

namespace Atlandiz.EnvanterVeZimmetYonetimi
{
    public partial class LicenseManage : DevExpress.XtraEditors.XtraForm
    {
        public LicenseManage()
        {
            InitializeComponent();
        }

        private void License_Load(object sender, EventArgs e)
        {

            if (AtlandizSmo.Shell() == true)
            {
                Serial.Text = Army.Serial;
                Serial.Enabled = false;
            }

            if (Serial.Enabled == false)
            {
                _btnCancelActivate.Visible = true;
            }
        }

        private void _btnKapat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void _btnTamam_Click(object sender, EventArgs e)
        {
            try
            {
                if (Serial.Enabled)
                {
                    if (Serial.Text.Length > 0 & Serial.Text != "LİSANS NUMARANIZI BURAYA GİRİNİZ")
                    {
                        if (Army.CheckInterConnection() == false)
                        {
                            MessageBox.Show("Atlandiz Lisans Sunucusuna erişim sağlanamıyor.\n" + Army.LicenseService.Url, "Erişim Sağlanamadı", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                            return;
                        }

                        DataSet LisansBilgisi = Army.LicenseService.GetCertificate(Army.ProgramBaseCode, Serial.Text, AtlandizSmo.GetSecureId());

                        switch (bool.Parse(LisansBilgisi.Tables[0].Rows[0][0].ToString()))
                        {
                            case true:
                                Serial.Text = LisansBilgisi.Tables[1].Rows[0][2].ToString();

                                IniAccessLayer ini = new IniAccessLayer(Army.ProgramBaseEnvironmentPath + "\\License.ini");
                                ini.Write("License", "ProgramBaseCode", LisansBilgisi.Tables[1].Rows[0][0].ToString());
                                ini.Write("License", "Program", LisansBilgisi.Tables[1].Rows[0][1].ToString());
                                ini.Write("License", "Serial", LisansBilgisi.Tables[1].Rows[0][2].ToString());
                                ini.Write("License", "SecureId", LisansBilgisi.Tables[1].Rows[0][3].ToString());
                                ini.Write("License", "ExpireDate", string.Format("{0:dd.MM.yyyy HH:mm:ss}", DateTime.Parse(LisansBilgisi.Tables[1].Rows[0][4].ToString())));
                                ini.Write("License", "Certificate", LisansBilgisi.Tables[1].Rows[0][5].ToString());
                                ini.Write("License", "LicenseClass", LisansBilgisi.Tables[1].Rows[0][6].ToString());
                                ini.Write("License", "isMovable", LisansBilgisi.Tables[1].Rows[0][7].ToString());
                                ini.Write("License", "CariUnvan", LisansBilgisi.Tables[1].Rows[0][8].ToString());
                                ini.Write("License", "CariEmail", LisansBilgisi.Tables[1].Rows[0][9].ToString());
                                ini.Write("License", "CariYetkili", LisansBilgisi.Tables[1].Rows[0][10].ToString());
                                ini.Write("License", "CariTel", LisansBilgisi.Tables[1].Rows[0][11].ToString());
                                ini.Write("License", "CariGsm", LisansBilgisi.Tables[1].Rows[0][12].ToString());

                                Army.GetLicenseInformation();
                                this.Close();
                                break;
                            case false:
                                MessageBox.Show("Lisanslama Hatası:\n" + LisansBilgisi.Tables[0].Rows[0][1].ToString(), "Geçersiz Lisans Bilgisi", MessageBoxButtons.OK, MessageBoxIcon.Error);
                                break;
                        }

                    }
                }
                else
                {
                    this.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message.ToString(), "Hata Oluştu", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            if (Army.CheckInterConnection() == false)
            {
                MessageBox.Show("Atlandiz Lisans Sunucusuna erişim sağlanamıyor.\n" + Army.LicenseService.Url, "Erişim Sağlanamadı", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }
            
            PopupAreYouSure DeactivateOnayi = new PopupAreYouSure();
            Army.EventMessage = "Bu İşlem; Lisans Aktivasyonunu iptal eder.\nDevam etmek istediğinize emin misiniz?";
            DeactivateOnayi.ShowDialog();

            if (Army.EventConfirm) {
                try
                {
                    Army.LicenseService.RemoveActivate(Army.ProgramBaseCode, Army.Serial, AtlandizSmo.GetSecureId());
                    Serial.Text = "";
                    File.Delete(Army.ProgramBaseEnvironmentPath + "\\License.ini");
                    Army.GetLicenseInformation();
                    Serial.Enabled = true;
                    _btnCancelActivate.Visible = false;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message.ToString(), "Hata Oluştu", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                }
            }
        }

        private void linkLabel1_LinkClicked_1(object sender, LinkLabelLinkClickedEventArgs e)
        {
            Process.Start("mailto:iletisim@atlandiz.com");
        }
    }
}